package soar.template.springboot.service.hello.configuration.redis;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPoolConfig;
import soar.template.springboot.service.hello.appconfig.redis.Config;
import soar.template.springboot.service.hello.appconfig.AppConfig;

@Configuration
class MyRedisConfiguration
{
    @Resource(name = "appconfig")
    private AppConfig appconfig;

    @Bean(name = "mainRedisConnectionFactory")
    @Primary
    public RedisConnectionFactory mainRedisConnectionFactory()
    {
        return create_factory(appconfig.redis.main);
    }
    
    private RedisConnectionFactory create_factory(Config config)
    {
        JedisPoolConfig pool_config = new JedisPoolConfig();
        pool_config.setBlockWhenExhausted(false);
        pool_config.setMaxTotal(100);
        pool_config.setTestOnBorrow(true);
        pool_config.setTimeBetweenEvictionRunsMillis(10 * 1000);
        pool_config.setMinEvictableIdleTimeMillis(30 * 1000);
        JedisConnectionFactory factory = new JedisConnectionFactory(
                pool_config);
        factory.setHostName(config.host);
        factory.setPort(config.port);
        return factory;
    }
}
